package com.dc.webview.simple

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.ValueCallback
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.dc.webview.R

class WebViewFragment: Fragment() {

    private lateinit var  webView: WebView
    private lateinit var tvJs: TextView
    private lateinit var tvJsArgs: TextView
    private lateinit var tvShowMsg: TextView

    private var data: String? = null

    companion object {
        const val DATA = "data"
        fun newInstance(data: String): WebViewFragment {
            val fragment = WebViewFragment()
            val args = Bundle()
            args.putString(DATA, data)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_webview, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        initData()
    }

    private fun initView(view: View) {
        webView = view.findViewById(R.id.webview)
        tvJs = view.findViewById(R.id.tv_androidcalljs)
        tvJsArgs = view.findViewById(R.id.tv_androidcalljsargs)
        tvShowMsg = view.findViewById(R.id.tv_showmsg)
        setWebView()

        tvJs.setOnClickListener {
//            webView.loadUrl("javascript:javacalljs()")
            webView.evaluateJavascript("javascript:javacalljs()", object : ValueCallback<String>{
                override fun onReceiveValue(value: String?) {
                    if (value != null) {
                        Log.e("---===", value)
                    }
                }
            })
        }

        tvJsArgs.setOnClickListener {
            val s = "Android传过来的参数"
//            webView.loadUrl("javascript:javacalljswith('$s')")
            webView.evaluateJavascript("javascript:javacalljswith('$s')", object : ValueCallback<String>{
                override fun onReceiveValue(value: String?) {
                    if (value != null) {
                        Log.e("---===", value)
                    }
                }
            })
        }
    }

    private fun setWebView() {
        val webSettings = webView.settings
        webSettings.javaScriptEnabled = true
        webSettings.setSupportZoom(true)
        webSettings.builtInZoomControls = true
        webSettings.allowFileAccess = true
        webView.webViewClient = WebViewClient()
        webView.loadUrl("file:///android_asset/html.html")
        webView.addJavascriptInterface(JavascriptInterface(this),"android")
    }

    private fun initData() {
        data = arguments?.getString(DATA)
    }
    fun getTvShowMsg(): TextView {
        return tvShowMsg
    }
}